//
//  STPlayerDetailController.swift
//  iPhone
//
//  Created by Toj on 11/17/23.
//

import Foundation

// MARK: - 懒加载(属性), 变量, 初始化init
class STPlayerDetailController: STBaseViewController {
    
    /** 播放器 **/
    private lazy var playerView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    
    /** <#NOTE#> **/
    private lazy var contentVC: STContentViewController = {
        let vc = STContentViewController()
        return vc
    }()
    
    /** <#NOTE#> */
    private lazy var officialView: UIImageView = {
        let view = UIImageView()
        view.image = "player_comment_official".to_image
        return view
    }()

    /** <#NOTE#> */
    private lazy var officialLabel: UILabel = {
        let view = UILabel()
        view.font = 12.to_rbtMediumFont
        view.text = "official".localized()
        view.textColor = "#0087EB".to_color
        return view
    }()
    
    /** <#NOTE#> */
    private let arrowWidth: CGFloat = 5
    private lazy var popoView: UIView = {
        let title = "DELETE"
        let font = 12.to_rbtMediumFont
        
        let deleteButton = UIButton()
        deleteButton.titleLabel?.font = font
        deleteButton.setTitle(title, for: .normal)
        deleteButton.setTitleColor(.white, for: .normal)
        let deleteWidth = title.cm_stringOneLineWidth(font: font) + 14
        print("-->startimes: deleteWidth: \(deleteWidth)")
        var frame = CGRect(x: 0, y: 0, width: deleteWidth, height: 20)
        deleteButton.frame = frame
        
        let view = UIView()
        view.addSubview(deleteButton)
        view.frame = frame
        return view
    }()
    
    deinit {
        print("-->startimes: \(st_className) func: deinit")
    }
}

// MARK: - 生命周期
extension STPlayerDetailController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let popover = Popover()
        popover.cornerRadius = 4
        popover.popoverType = .left
        popover.popoverColor = .black
        popover.showBlackOverlay = false
        popover.didShowHandler = nil
        popover.didDismissHandler = nil
        popover.arrowSize = CGSize(width: 8, height: arrowWidth)
        popover.show(popoView, fromView: officialView)
    }
}
    
extension STPlayerDetailController: UIPopoverPresentationControllerDelegate {
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
    }
}

// MARK: - 初始化设置
private extension STPlayerDetailController {
    
    func setupMakeAddSubviews() {
        addChild(contentVC)
        
        view.addSubview(contentVC.view)
        view.addSubview(playerView)
        
        view.addSubview(officialView)
        officialView.addSubview(officialLabel)
    }
    
    func setupMakeLayoutSubviews() {
        playerView.snp.makeConstraints({ make in
            make.top.left.right.equalTo(view)
            make.height.equalTo(CGFloat.dv_width.zoom16_9)
        })
        
        contentVC.view.snp.makeConstraints { make in
            make.top.equalTo(playerView.snp.bottom)
            make.bottom.left.right.equalTo(view)
        }
        officialView.snp.remakeConstraints { make in
            make.height.equalTo(16)
            make.width.equalTo(56)
            make.center.equalTo(playerView)
        }
        officialLabel.snp.makeConstraints { make in
            make.height.equalTo(16)
            make.width.equalTo(40)
            make.centerY.right.equalTo(officialView)
        }
    }
    
    func setupMakeInitialize() {
        tt_isNavigationBarHidden = true
        navigationBar.isHidden = true
        view.backgroundColor = .white
    }
}
